package com.gjy.concurrency.h1;

import org.junit.jupiter.api.Test;

import java.util.concurrent.*;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-30 16:25:14
 */
public class PoolTest2 {

    public static void main(String[] args) {
        ThreadPoolExecutor pool = new CustomerPool(3, 3,
                1, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

        for (int i = 0; i < 3; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName());
                        int nextInt = ThreadLocalRandom.current().nextInt(100);
                        TimeUnit.MILLISECONDS.sleep(nextInt);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            /*pool.execute(() -> {
                try {
                    System.out.println(Thread.currentThread().getName());
                    int nextInt = ThreadLocalRandom.current().nextInt(100);
                    TimeUnit.MILLISECONDS.sleep(nextInt);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });*/
        }

        pool.shutdown();
    }

    @Test
    public void test1() {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3, 5, 1, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );


        for (int i = 0; i < 10; i++) {
            pool.submit(() -> {
                String name = Thread.currentThread().getName();
                System.out.println(name);
            });
        }
        pool.shutdown();
    }
}
